{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "SVG path loading unavailable!\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/musy/soft/anaconda3/lib/python3.6/site-packages/trimesh/path/exchange/svg_io.py\", line 18, in <module>\n",
      "    from svg.path import parse_path\n",
      "ModuleNotFoundError: No module named 'svg'\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4d1ddaee5f6d4d8bbfd6afef031832bb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import trimesh\n",
    "import numpy as np\n",
    "from vtkplotter import show, Plane, printc, download\n",
    "\n",
    "# load the mesh from filename, file objects are also supported\n",
    "f = download('https://github.com/mikedh/trimesh/raw/master/models/featuretype.STL')\n",
    "mesh = trimesh.load_mesh(f)\n",
    "\n",
    "# get a single cross section of the mesh\n",
    "mslice = mesh.section(plane_origin=mesh.centroid, plane_normal=[0,0,1])\n",
    "\n",
    "pl = Plane(mesh.centroid, normal=[0,0,1], sx=6, sy=4, alpha=0.3)\n",
    "\n",
    "slice_2D, to_3D = mslice.to_planar()\n",
    "\n",
    "# show objects on N=2 non-synced renderers:\n",
    "show(mesh, pl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[3m\u001b[32mnr. of sections: 11\u001b[0m\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c59b2ba849e44e2583fd2c0f4dc64071",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# if we wanted to take a bunch of parallel slices, like for a 3D printer\n",
    "# we can do that easily with the section_multiplane method\n",
    "# we're going to slice the mesh into evenly spaced chunks along z\n",
    "# this takes the (2,3) bounding box and slices it into [minz, maxz]\n",
    "z_extents = mesh.bounds[:,2]\n",
    "# slice every .125 model units (eg, inches)\n",
    "z_levels  = np.arange(*z_extents, step=0.125)\n",
    "\n",
    "# find a bunch of parallel cross sections\n",
    "sections = mesh.section_multiplane(plane_origin=mesh.bounds[0], \n",
    "                                   plane_normal=[0,0,1], \n",
    "                                   heights=z_levels)\n",
    "N = len(sections)\n",
    "printc(\"nr. of sections:\", N, c='green')\n",
    "\n",
    "# summing the array of Path2D objects will put all of the curves\n",
    "# into one Path2D object, which we can plot easily\n",
    "combined = np.sum(sections)\n",
    "\n",
    "# show objects in N synced renderers:\n",
    "show(combined)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37fcbf8a0e1c403ca5378e84cdc0046c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# the medial axis is available for closed Path2D objects\n",
    "show(slice_2D + slice_2D.medial_axis())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
